using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace SBPweb.Controls.Windows
{
	public class FormNotifier
	{
		private FormNotifier()
		{
		}

		[StructLayout(LayoutKind.Sequential)]
		protected struct FLASHWINFO
		{
			public UInt32 cbSize;
			public IntPtr hwnd;
			public UInt32 dwFlags;
			public UInt32 uCount;
			public UInt32 dwTimeout;
		}

		//Stop flashing. The system restores the window to its original state. 
		public const UInt32 FLASHW_STOP = 0;
		//Flash the window caption. 
		public const UInt32 FLASHW_CAPTION = 1;
		//Flash the taskbar button. 
		public const UInt32 FLASHW_TRAY = 2;
		//Flash both the window caption and taskbar button.
		//This is equivalent to setting the FLASHW_CAPTION | FLASHW_TRAY flags. 
		public const UInt32 FLASHW_ALL = 3;
		//Flash continuously, until the FLASHW_STOP flag is set. 
		public const UInt32 FLASHW_TIMER = 4;
		//Flash continuously until the window comes to the foreground. 
		public const UInt32 FLASHW_TIMERNOFG = 12;

		[DllImport("user32.dll")]
		static extern Int32 FlashWindowEx(ref FLASHWINFO pwfi);

		public static bool FlashWindowEx(IntPtr hWnd)
		{
			return FlashWindowEx(hWnd, FLASHW_ALL | FLASHW_TIMERNOFG, 20, 0);
		}

		public static bool FlashWindowEx(IntPtr hWnd, UInt32 flags, UInt32 count, UInt32 timeout)
		{
			FLASHWINFO fInfo = new FLASHWINFO();

			fInfo.cbSize = Convert.ToUInt32(Marshal.SizeOf(fInfo));
			fInfo.hwnd = hWnd;
			fInfo.dwFlags = flags;
			fInfo.uCount = count;
			fInfo.dwTimeout = timeout;

			return (FlashWindowEx(ref fInfo) == 0);
		}

		public static void Nudge(System.Windows.Forms.Form form)
		{
			foreach (Control c in form.Controls)
			{
				c.SuspendLayout();
			}

			// Store the original location of the form.
			int xCoord = form.Left;
			int yCoord = form.Top;

			// An integer for storing the random number each time
			int rnd = 0;

			// Instantiate the random generation mechanism
			Random RandomClass = new Random();

			for (int i = 0; i <= 300; i++)
			{
				rnd = RandomClass.Next(xCoord + 1, xCoord + 15);
				form.Left = rnd;
				rnd = RandomClass.Next(yCoord + 1, yCoord + 15);
				form.Top = rnd;
			}

			// Restore the original location of the form
			form.Left = xCoord;
			form.Top = yCoord;

			foreach (Control c in form.Controls)
			{
				c.ResumeLayout();
			}
		}


	}

}
